package excel;

import org.junit.Test;
import vip.ipav.poi.excel.writer.Excel2007Writer;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * Created by 89003522 on 2017/11/21.
 */
public class ExcelEventWriter {

    @Test
    public void commonWriter() throws Exception{
        long date = System.currentTimeMillis();
        String file = "D:/test-write.xlsx";
        List<List<Object>> list = new ArrayList<>();
        for (int rownum = 0; rownum < 100000; rownum++){
            List<Object> tmp = new ArrayList<>(7);
            tmp.add("第 " + (rownum+1) + " 行");
            tmp.add(125);
            tmp.add(0.12);
            tmp.add("12:12:23");
            tmp.add("2017-10-11 12:12:23");
            tmp.add(true);
            tmp.add(false);
            list.add(tmp);
        }
        System.out.println(System.currentTimeMillis() - date);
        date = System.currentTimeMillis();

        new Excel2007Writer(file).process(list);
        System.out.println(System.currentTimeMillis() - date);
    }

    @Test
    public void outWriter() throws Exception{
        long date = System.currentTimeMillis();
        String file = "D:/test-write.xlsx";
        List<List<Object>> list = new ArrayList<>();
        for (int rownum = 0; rownum < 10; rownum++){
            List<Object> tmp = new ArrayList<>(7);
            tmp.add("第 " + (rownum+1) + " 行");
            tmp.add(125);
            tmp.add(0.12);
            tmp.add("12:12:23");
            tmp.add("2017-10-11 12:12:23");
            tmp.add(true);
            tmp.add(false);
            tmp.add("Hello World 世界你好");
            list.add(tmp);
        }
        System.out.println(System.currentTimeMillis() - date);
        date = System.currentTimeMillis();

        new Excel2007Writer(new FileOutputStream(file)).process(list);
        System.out.println(System.currentTimeMillis() - date);
    }

    @Test
    public void binaryWriter() throws Exception{
        long date = System.currentTimeMillis();
        String file = "D:/test-write.xlsx";
        List<List<Object>> list = new ArrayList<>();
        for (int rownum = 0; rownum < 100000; rownum++){
            List<Object> tmp = new ArrayList<>(7);
            tmp.add("第 " + (rownum+1) + " 行");
            tmp.add(125);
            tmp.add(0.12);
            tmp.add("12:12:23");
            tmp.add("2017-10-11 12:12:23");
            tmp.add(true);
            tmp.add(false);
            tmp.add("Hello World 世界你好");
            list.add(tmp);
        }
        System.out.println(System.currentTimeMillis() - date);
        date = System.currentTimeMillis();

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new Excel2007Writer(out).process(list);
        System.out.println(System.currentTimeMillis() - date);

        out.writeTo(new FileOutputStream(file));
        System.gc();
    }
}
